import logging
import time
import os

class MyLogger():

    def __init__(self):
        # 初始化日志记录器
        self.logger = logging.getLogger()
        # 设置日志记录器的等级
        self.logger.setLevel(logging.DEBUG)
        # 控制台的输出处理器
        sh = logging.StreamHandler()
        sh.setLevel(logging.INFO)

        # 处理日志文件名
        log_filename = "{}.log".format(time.strftime("%Y-%m-%d"))
        # 处理日志文件保存地址
        file_path = os.path.dirname(__file__)  # D:\PycharmProjects\PythonProject\UIAotoText\baseTools
        file_path = os.path.join(file_path, "..", "log")  # D:\PycharmProjects\PythonProject\UIAotoText\baseTools\..\log
        if not os.path.isdir(file_path):
            os.makedirs(file_path)

        fh = logging.FileHandler(filename=os.path.join(file_path, log_filename), encoding='utf-8', mode="w")
        fh.setLevel(logging.INFO)

        # 设置日志的输出格式
        fm = logging.Formatter("%(asctime)s|%(levelname)s|%(filename)s|%(lineno)s|%(message)s")
        # 设置处理器日志输出格式
        sh.setFormatter(fm)
        fh.setFormatter(fm)

        # 将处理器添加到记录器中
        self.logger.addHandler(fh)
        self.logger.addHandler(sh)

logger = MyLogger().logger



